package com.gjt.stream;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * Created with Intel IDEA.
 * 书写器(java.io.Writer)：指定写字符的api;
 * 重要的具体子类OutputStreamWriter:接收Unicode字节，使用指定的编码方式将字符转换成字节，将字节写入底层输出流
 *            原始阅读器FileWriter,
 *           StringWriter,CharArrayWriter:java内部使用
 *  注意：写完之后一定刷新流
 *  过滤器书写器：BufferWriter(串联到底层书写器，默认缓冲大小8192字符),PrintWriter
 *
 *
 * @Auther: gjt
 * @Date: 2024/3/20 8:58
 */
public class Write {

    private static void write(FileOutputStream out) throws IOException {
        char[] list={'a','b','c'};
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
        outputStreamWriter.write(list, 0,2);
//        outputStreamWriter.write("abc",0,2);
        outputStreamWriter.flush();
    }


    // BufferedWriter的newLine：插入一个与平台有关的行分隔符字符串
    private static void write1(FileOutputStream out) throws IOException {
        char[] list={'a','b','c'};
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
        BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
        bufferedWriter.write(list);
        bufferedWriter.newLine();
        //回车和换行
        bufferedWriter.write("\n\r");
        bufferedWriter.write("def");
//        outputStreamWriter.write("abc",0,2);
        bufferedWriter.flush();
    }


    public static void main(String[] args) {
        try(FileOutputStream fileWriter =new FileOutputStream("C:/Users/23152/Desktop/hello.txt")){
//            write(fileWriter);
            write1(fileWriter);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
